FlexPageTest = TestCase("FlexPageTest");
FlexPageTest.prototype.setUp = function() {
	var mock_cp = {
			last_flow: [],
			isPartLoaded: function(partId) {
				assertTrue( [1001,1002,1003,1004].indexOf(partId) >= 0 );
				return false; 
			},
			clearScreen: function(page) {  
				assertEquals("FlexPage interface",page.objType);
				var partsConfig = page.getPartsConfig();
				assertArray(partsConfig);
				assertEquals(4,partsConfig.length);
				assertEquals("part_c.js", partsConfig[1].PartFile);
				this.last_flow.push("clearScreen");
			},
			recordPart: function(part) { 
				assertTrue( [1001,1002,1003,1004].indexOf(part.getConfig().Id) >= 0 );
				this.last_flow.push("recordPart:" + part.getConfig().Id);
			},
			attachParts: function(page) { 
				assertEquals("FlexPage interface", page.objType ); 
				var partsConfig = page.getPartsConfig();
				assertArray(partsConfig);
				assertEquals(4,partsConfig.length);
				assertEquals("part_c.js", partsConfig[1].PartFile);
				this.last_flow.push("attachParts");
			},
			loadWrapperCls: function() {
				this.last_flow.push("loadWrapperCls");
			},
			hideLoadingIcon: function() {
				
			},
			getWrapperManager: function(){
				return {
					getWrapperInstance: function(wrapperType) {
						assertTrue(["front_movie","scroll_area","message_board","news_pager" ].indexOf(wrapperType) >= 0);
						return {
							wrap: function(){  }
						};
					}
				};
			}
	};	
	window.contentPanel = mock_cp;
	this.page = new com.yogi.core.FlexPage();
	window.page_config = [ 
	                    [ { Id: 1001, PartFile:"part_a.js",WrapperParam:"{ \"type\":\"front_movie\" }" },
	                      { Id: 1003, PartFile:"part_c.js",WrapperParam:"{ \"type\":\"scroll_area\" }" },
	                      { Id: 1002, PartFile:"part_b.js",WrapperParam:"{ \"type\":\"message_board\" }" },
	                      { Id: 1004, PartFile:"part_d.js",WrapperParam:"{ \"type\":\"news_pager\" }" }],
	                    [[3],[0,1,2]]   ];	
	this.page.test_func("parsePartsInfo",[ window.page_config]);
	var res_part_map = this.page.test_func("m_res_part_map");
	assertEquals(1004, res_part_map["part_d.js"].Id);
	this.page.test_setter("resources_loader",function(urls,cb,scope) {
		window.contentPanel.last_flow.push("resources_loader:" + urls);
		cb.apply(scope,[ urls ]);
	});
	this.page.test_setter("resources_getter", function(url) {
		return $("<div id=\"div_res_partId\"/>");
	});
};
FlexPageTest.prototype.testFlexPage_parsePartsInfo = function(){
	var page = new com.yogi.core.FlexPage();
	var page_config = window.page_config;
	page.test_func("parsePartsInfo",[page_config]);
	var res_part_map = page.test_func("m_res_part_map");
	assertObject(res_part_map);
	assertTrue(res_part_map.hasOwnProperty("part_a.js"));
	assertTrue(res_part_map.hasOwnProperty("part_b.js"));
	assertTrue(res_part_map.hasOwnProperty("part_c.js"));
	assertTrue(res_part_map.hasOwnProperty("part_d.js"));	
	
	assertEquals(1004, res_part_map["part_d.js"].Id);
	assertEquals("part_d.js", res_part_map["part_d.js"].PartFile);
	
	var partid_seq_map = page.test_func("m_partid_seq_map");
	assertObject(partid_seq_map);
	assertEquals(0, partid_seq_map[1001]);
	assertEquals(1,partid_seq_map[1003]);
	assertEquals(2,partid_seq_map[1002]);
	assertEquals(3,partid_seq_map[1004]);
	
	var needload_parts = page.test_func("m_needload_parts");
	assertArray(needload_parts);
	assertEquals(4, needload_parts.length);	
};
FlexPageTest.prototype.testLoadParts = function() {
	window.contentPanel.last_flow = [];
	var needload_parts = this.page.test_func("m_needload_parts");
	assertArray(needload_parts);
	assertEquals(4, needload_parts.length);
	this.page.loadParts();
	//console.log(window.contentPanel.last_flow);
	assertEquals('clearScreen', window.contentPanel.last_flow[0]);
	assertEquals('resources_loader:part_d.js', window.contentPanel.last_flow[1]);
	assertEquals('recordPart:1004', window.contentPanel.last_flow[2]);
	assertEquals('resources_loader:part_a.js,part_c.js,part_b.js', window.contentPanel.last_flow[3]);
	assertEquals('recordPart:1001', window.contentPanel.last_flow[4]);
	assertEquals('recordPart:1003', window.contentPanel.last_flow[5]);
	assertEquals('recordPart:1002', window.contentPanel.last_flow[6]);
	window.contentPanel.last_flow = [];
	this.page.test_setter("m_needload_parts", "[]");
	this.page.loadParts();
	assertEquals('attachParts', window.contentPanel.last_flow[0]);
};
FlexPageTest.prototype.testCheckPartsExistency = function() {
	$.extend(window.contentPanel, {
		isPartLoaded: function(partId) {
			return  [1001,1002,1004].indexOf(partId) >= 0;
		}
	});
	var page = new com.yogi.core.FlexPage(window.contentPanel);
	var page_config = window.page_config;
	page.test_func("parsePartsInfo",[page_config]);
	page.test_func("checkPartsExistency");
	page.test_func("m_contentPanel", window.contentPanel);
	var m_needload_parts = page.test_func("m_needload_parts");
	assertEquals(1, m_needload_parts.length);
	assertEquals(1003, m_needload_parts[0].Id);
};
FlexPageTest.prototype.test_loadResourcesByGroup = function() {
	window.contentPanel.last_flow = [];
	this.page.test_func("loadResourcesByGroup");
	//console.log(window.contentPanel.last_flow);
	assertEquals('resources_loader:part_d.js', window.contentPanel.last_flow[0]);
	assertEquals('recordPart:1004', window.contentPanel.last_flow[1]);
	assertEquals('resources_loader:part_a.js,part_c.js,part_b.js', window.contentPanel.last_flow[2]);
	assertEquals('recordPart:1001', window.contentPanel.last_flow[3]);
	assertEquals('recordPart:1003', window.contentPanel.last_flow[4]);
	assertEquals('recordPart:1002', window.contentPanel.last_flow[5]);
	assertEquals('attachParts', window.contentPanel.last_flow[6]);
	assertEquals('attachParts', window.contentPanel.last_flow[7]);
};
FlexPageTest.prototype.test_inner_isPartReserved = function() {
	var ret = this.page.test_func("inner_isPartReserved", [1008]);
	assertFalse(ret);
	this.page.setReservePartConfig( [1003,1004] );
	ret = this.page.test_func("inner_isPartReserved",[1003]);
	assertTrue(ret);
	ret = this.page.test_func("inner_isPartReserved",[1004]);
	assertTrue(ret);
};

FlexPageTest.prototype.test_load = function() {
	window.contentPanel.last_flow = [];
	this.page.test_setter("json_loader", function(url,cb) {
		cb( { data:{Context:window.page_config}},"success");
	});
	this.page.load("test_catalog");
	assertEquals('loadWrapperCls', window.contentPanel.last_flow[0]);
};
